#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <error.h>
#include <pthread.h>
#include <sys/select.h>
#define PORT 8888
#define SERVER_IP "0.0.0.0"
#define LISTEN_BYTES 128
#define MASSAGE_SIZE 64
void *worker_func(void *arg)
{
    pthread_detach(pthread_self());
    int acceptfd = *(int *)arg;
    /*接收信息*/
    char recMessage[MASSAGE_SIZE];
    memset(recMessage, 0, sizeof(recMessage));
    /*发送信息*/
    char sendMessage[MASSAGE_SIZE];
    memset(sendMessage, 0, sizeof(sendMessage));
    strncpy(sendMessage, "456", sizeof(sendMessage) - 1);

    int ret = 0;
    while (1)
    {
        /*第五步：通信*/

        /*读取客户端发送的信息*/
        ret = read(acceptfd, recMessage, sizeof(recMessage));
        if (ret <= 0)
        {
            perror("read error");
            close(acceptfd);
            break;
        }
        else
        {
            printf("client massage:%s\n", recMessage);
        }

        ret = write(acceptfd, sendMessage, sizeof(sendMessage));
        if (ret <= 0)
        {
            perror("write error");
            close(acceptfd);
            break;
        }
    }

    pthread_exit(NULL);
}
int main()
{
    pthread_t tid;
    /*构建服务器的步骤*/
    /*第一步：socket：创建套接字*/
    /*三个参数:协议(ipv4/ipv6),SOCK_STREAM/SOCK_DGRAM(tcp/udp),默认0*/
    /*返回值：文件描述符*/
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket error");
        exit(-1);
    }
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    /*inet_pton函数的三个参数：协议(ipv4/ipv6),IP,写入的地址(传出参数)*/
    int ret = inet_pton(AF_INET, SERVER_IP, (void *)&serverAddr.sin_addr.s_addr);
    if (ret == -1)
    {
        perror("inet_pton error");
        exit(-1);
    }
    /*第二步:bind绑定本地信息*/
    /*bind三个参数：sockfd;sockaddr_in结构体包括：协议(ipv4/ipv6)，IP,端口;sizeof（第二个参数）*/

    int opt = 1;
    /*在进行bind函数前进行端口复用选择*/
    /*setsockopt函数5个参数：sockfd；SOL_SOCKET;SO_REUSEADDR;opt=1(复用)/0(不复用);第四个参数大小*/
    ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
    if (ret == -1)
    {
        perror("setsockopt error");
        exit(-1);
    }
    ret = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    if (ret == -1)
    {
        perror("bind error");
        exit(-1);
    }
    /*第三步：listen监听连接*/
    /*listen两个参数：sockfd;监听字节数*/
    listen(sockfd, LISTEN_BYTES);

    struct sockaddr_in clientAddr;
    memset(&clientAddr, 0, sizeof(clientAddr));
    socklen_t clientAddrSize;
    int acceptfd = 0;

    while (1)
    {
        acceptfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrSize);
        if (acceptfd == -1)
        {
            perror("accept error");
            exit(-1);
        }
#if 1
        ret = pthread_create(&tid, NULL, worker_func, &acceptfd);
        if (ret == -1)
        {
            perror("pthread_create error");
            exit(-1);
        }
#endif
    }

    sleep(2);
    /*第七步：关闭*/

    close(sockfd);
    return 0;
}